package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-09 12:07
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 通过username查找User，并授权包括角色，权限
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        //查找user，一句sql查询user
        User user=userDao.findByUsername(username);
        if(user==null){
            return null;
        }
        //查询role，并赋予user
        /*Set<Role> roles=roleDao.findByUserid(user.getId());
        if(roles!=null&&roles.size()>0) {
            for (Role role : roles) {
                //查询permission并赋予role
                Set<PermissionService> permissions = permissionDao.findByRoleid(role.getId());
                if(permissions!=null&&permissions.size()>0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }*/
        return user;
    }


    /**
     * 修改密码
     * @param formData
     */
    @Override
    public void changecode(Map<String,String> formData) throws RuntimeException {
        String againcode = formData.get("againcode");
        String newcode = formData.get("newcode");
        String username = formData.get("username");

        if (newcode.equals(againcode)) {
            userDao.changecode(formData);
        } else {
            throw new RuntimeException("新密码不一致，请重新输入");
        }

    }

    /**
     * 查询用户菜单
     * @param username
     * @return
     */
    @Override
    public List<Map> findMenu(String username) {
        List<Menu> list=userDao.findMenu(username);
        //要将查到的数据二次封装为前台展示数据
        List<Map> menuList =new ArrayList<>();
        Map map=new HashMap<>();
        map.put("path","1");
        map.put("title","工作台");
        map.put("icon","fa-dashboard");
        map.put("children",null);
        menuList.add(map);
        //根据level判断是否存在chidren菜单,再更据菜单id遍历添加到children
        for(Menu m:list){
            if("1".equals(m.getLevel())){
                List<Map> children =new ArrayList<>();
                for(Menu n:list){
                    if("2".equals(n.getLevel())&&n.getParentMenuId().equals(m.getId())){
                        //将二级菜单相应信息整合
                        Map childmap=new HashMap();
                        childmap.put("path",n.getPath());
                        childmap.put("title",n.getName());
                        childmap.put("linkUrl",n.getLinkUrl());
                        childmap.put("children","null");
                        children.add(childmap);
                    }
                }
                //chidren 添加完毕后，再进行二次封装，变为前端可显示的数据类型,前端只要一级菜单和工作台
                Map map1=new HashMap<>();
                map1.put("path",m.getPath());
                map1.put("title",m.getName());
                map1.put("icon",m.getIcon());
                map1.put("children",children);
                menuList.add(map1);
            }
        }
        return menuList;
    }

    /*工作台
    {
        "path": "1",
            "title": "工作台",
            "icon":"fa-dashboard",
            "children": []
    }*/
   /* "path": "/2-1",
            "title": "会员档案",
            "linkUrl":"member.html",
            "children":[]*/

   /*"path": "2",
           "title": "会员管理",
           "icon":"fa-user-md",
           "children":*/
}
